/**
 * pharma-ws - 19 mars 2011 guillaume
 */
package org.bspv.pharma.ws.endpoint.management.order;

import org.bspv.pharma.process.ManagementProcessService;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.xpath.XPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;

/**
 * Endpoint pour la création d'une commande.
 * 
 * @author guillaume
 * 
 */
@Endpoint
public class OrderCreationEndpoint {

    /**
     * Namespace URI.
     */
    private static final String NAMESPACE_URI = "http://bspv-pharma.ws/or/schemas";

    /**
     * Expression XPath de l'identifiant de pharmacie.
     */
    private XPath drugstoreIdExpression;
    /**
     * Expression XPath du commentaire de la commande.
     */
    private XPath orderCommentExpression;
    /**
     *  Expression XPath de la description de la commande.
     */
    private XPath orderDescriptionExpression;
    /**
     * Expression XPath du commentaire de l'évènement de création.
     */
    private XPath commentExpression;

    /**
     * Process service.
     */
    private ManagementProcessService managementProcessService;

    /**
     * Constructeur.
     * 
     * @param managementProcessService
     *            Service processus
     * @throws JDOMException
     *             Exception
     */
    @Autowired
    public OrderCreationEndpoint(ManagementProcessService managementProcessService) throws JDOMException {

        this.managementProcessService = managementProcessService;

        Namespace namespace = Namespace.getNamespace("or", NAMESPACE_URI);

        drugstoreIdExpression = XPath.newInstance("");
        drugstoreIdExpression.addNamespace(namespace);
        
        orderCommentExpression = XPath.newInstance("");
        orderCommentExpression.addNamespace(namespace);
        
        orderDescriptionExpression = XPath.newInstance("");
        orderDescriptionExpression.addNamespace(namespace);
        
        commentExpression = XPath.newInstance("//or:comment");
        commentExpression.addNamespace(namespace);
    }

    /**
     * Méthode de prise en charge de la requête.
     * @param orderCreationRequest Requete
     * @throws Exception Exception
     */
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "OrderCreationRequest")
    public void handleOrderCreationRequest(@RequestPayload Element orderCreationRequest) throws Exception {
        // Extraction des données de la requete
        String comment = commentExpression.valueOf(orderCreationRequest);
        Long drugstoreId = Long.decode(drugstoreIdExpression.valueOf(orderCreationRequest));
        String orderComment = orderCommentExpression.valueOf(orderCreationRequest);
        String orderDescription = orderDescriptionExpression.valueOf(orderCreationRequest);
        // Récupération de l'utilisateur à l'origine de la demande
        Long userId = null;// TODO Récupérer l'identifiant de l'utilisateur
        // Appel du service processus
        managementProcessService.createOrder(drugstoreId, orderComment, orderDescription, comment, userId);
    }
}
